package com.architecture.web.jsf.controller;

import java.io.InputStream;

import javax.faces.event.ActionEvent;
import com.architecture.controller.Controller;
import com.architecture.entity.Entity;
import com.architecture.util.bundle.BundleUtility;

/**
 * <p>Base of all jsf controllers.</p>
 * @author JR
 * @param <E>
 */
public abstract class JSFController<E extends Entity> extends Controller<E>{
		
		private static BundleUtility messages;
	
		static{
			final InputStream in = JSFController.class.getClassLoader().getResourceAsStream("messages.properties");
			messages = BundleUtility.getInstance().build(in);	
		}
		
		/**
		 * <p> Clear data.</p>
		 */
		public void clear(){
			
		}
		
		/**
		 * <p>Returns the navigation rule used to open the home page.</p>
		 * @return <code>String</code>
		 */
		public final String getHomePage(){
			return this.getClass().getSimpleName().concat(this.getMessage("com.architecture.controller.action.home"));
		}
		
		/**
		 * <p>Opens insert page.</p>
		 * @return <code>String</code>
		 */
		public final String getIncludePage(){
			return this.getClass().getSimpleName().concat(this.getMessage("com.architecture.controller.action.insert"));
		}
		
		/**
		 * <p>Returns the navigation rule to open the update page.</p>
		 * @return <code>String</code>
		 */
		public String getUpdatePage(){
			return this.getClass().getSimpleName().concat(this.getMessage("com.architecture.controller.action.update"));
		}
		
		/**
		 * <p>Returns the navigation rule used to open the detail page.</p>
		 * @return <code>String</code>
		 */
		public final String getDetailPage(){
			return this.getClass().getSimpleName().concat(this.getMessage("com.architecture.controller.action.detail"));
		}
		
		/**
		 * <p>Calls delete action.</p>
		 * @param event
		 */
		public void delete(ActionEvent event){
			super.delete(this.getForm().getView().getIdentifier());
		}
		
		/**
		 * <p>Calls update action.</p>
		 * @param event
		 */
		public void update(ActionEvent event){
			super.update(this.getForm().getView());
		}
		
		/**
		 * <p>Calls create action.</p>
		 * @param event
		 */
		public void create(ActionEvent event){
			super.create(this.getForm().getView());
		}
		
		/**
		 * <p>Returns a message using a key.</p>
		 * @param key
		 * @return <code>String</code>
		 */
		protected String getMessage(final String key){
			return getMessagesBundle().getValue(key);
		}
		
		/**
		 * <p>Returns a message using a key and format the end result.</p>
		 * @param key
		 * @param values
		 * @return <code>String</code>
		 */
		protected String getMessage(final String key,Object...values){
			return getMessagesBundle().getValue(key, values);
		}
		
		/**
		 * <p>Returns the bundle <code>messages.properties</code>.</p>
		 * @return BundleUtility
		 */
		protected BundleUtility getMessagesBundle(){
			return messages;
		}
		
}

